Skip to content

ftell() not consistent in C11#8360

Open
damorinCan wants to merge 3 commits intocppcheck-opensource:mainfrom
damorinCan:CWE474_ftell
Open

ftell() not consistent in C11#8360
damorinCan wants to merge 3 commits intocppcheck-opensource:mainfrom
damorinCan:CWE474_ftell

Conversation

@damorinCan
Copy link
Copy Markdown

@chrchr-github
Copy link
Copy Markdown
Collaborator

Thanks for your contribution.
As far as I can tell, this has nothing to do with Windows 11, but rather the C11 spec of ftell(), right? So the title of the PR should be adjusted.
Please add a test.

@damorinCan damorinCan changed the title ftell() have a different behavior in Windows 11 than before. ftell() not consistent in C11 Mar 21, 2026
@damorinCan
Copy link
Copy Markdown
Author

So the title of the PR should be adjusted.

Title now including reference to C11 and removed reference to Windows 11.

Please add a test.

I added a test in test/testio.cpp. Anything missing in it ?

@chrchr-github
Copy link
Copy Markdown
Collaborator

Please add a test.

I added a test in test/testio.cpp. Anything missing in it ?

Thanks, I must have missed that.

@chrchr-github
Copy link
Copy Markdown
Collaborator

Ping @danmar

Copy link
Copy Markdown
Collaborator

@danmar danmar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel we need to have a ticket for this in trac

it feels like it should be added to the release notes as a "new check" ?

Comment thread lib/checkio.cpp Outdated
case Filepointer::Operation::UNIMPORTANT:
if (f.mode == OpenMode::CLOSED)
useClosedFileError(tok);
if (isftell && windows && f.read_mode == Filepointer::ReadMode::READ_TEXT && printPortability)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the error message says something about the C11 standard. Nothing about windows.

Copy link
Copy Markdown
Author

@damorinCan damorinCan Apr 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add a comment about this reference to this Microsoft page:

https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/ftell-ftelli64?view=msvc-170

An update for the error message with this, would it be OK ?

According to Microsoft, the value returned by ftell may not reflect the physical byte offset for streams opened in text mode, because text mode causes carriage return-line feed translation. See also 7.21.9.4 in C11 standard.".

I will remove the reference to _wfopen(), it's not part of the C++ standard.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry.. my confusion was because you wrote && windows &&.

Now that I understand the issue better I think it's better to be clear that it's a standard problem.

Comment thread test/testio.cpp Outdated
@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Apr 13, 2026

I have tried to add some descriptions about some of our checkers here:
https://github.com/danmar/cppcheck/tree/main/man/checkers

Ideally that would contain descriptions for all our checkers but due to lack-of-resources it does not.

If you can add a corresponding file for this checking that would be great.

- Fix the uncrustify check (removed https reference to Microsoft).

New changes:
- Added missing string related to new check
- Added checker description for ftellTextModeFile
- Updated copyright.
@sonarqubecloud
Copy link
Copy Markdown

@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Apr 24, 2026

We need a ticket in trac for this.

Can you please tell me what you think the summary and description should be and we can create it..

@@ -0,0 +1,52 @@
# ftellModeTextFile

**Message**: According to Microsoft, the value returned by ftell may not reflect the physical byte offset for streams opened in text mode, because text mode causes carriage return-line feed translation. See also 7.21.9.4 in C11 standard.<br/>
Copy link
Copy Markdown
Collaborator

@danmar danmar Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not according to Microsoft. It's according to the C standard.

Sorry I was confused before.

I think it would make sense to quote the text from the C standard in the description in this document and highlight the part that says its file position indicator contains unspecified information.

The ftell function obtains the current value of the file position indicator for the stream
pointed to by stream. For a binary stream, the value is the number of characters from
the beginning of the file. For a text stream, its file position indicator contains unspecified
information, ...

Comment thread lib/checkio.h
/* -*- C++ -*-
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2025 Cppcheck team.
* Copyright (C) 2007-2026 Cppcheck team.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this diff is not needed. we don't tweak it manually.

Comment thread test/testio.cpp
" if (f)\n"
" {\n"
" fseek(f, 0, SEEK_END);\n"
" (void)ftell(f);\n"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think a warning here is a false positive. We can clearly see that nothing unexpected happens. The program will do what the developer expects. An assignment of an unknown or global variable pos = ftell(f); would make it more dangerous.

if (f)
{
fseek(f, 0, SEEK_END);
printf( "Offset %d\n", ftell(f);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make the example code more "buggy"? If the program writes the offset for debugging purposes this output might be 100% fine. If the program writes "File size: %d\n" instead it would be a bit more clear it's a bug imho. If you have better suggestions to make it more "buggy" feel free to do it..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants